#
# Created by Administrator on 2021/7/28 AT 21:54.
#
# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def removeDuplicateNodes(self, head: ListNode) -> ListNode:
        if not head:
            return head
        occurred = {head.val}
        pos = head
        # 枚举前驱节点
        while pos.next:
            # 当前待删除节点
            cur = pos.next
            if cur.val not in occurred:
                occurred.add(cur.val)
                pos = pos.next
            else:
                pos.next = pos.next.next
        return head


if __name__ == '__main__':
    n1, n2, n3, n4, n5, n6 = ListNode(1), ListNode(2), ListNode(3), ListNode(3), ListNode(2), ListNode(1)
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    n5.next = n6
    sol = Solution()
    ansNode = sol.removeDuplicateNodes(n1)
    while ansNode:
        print(ansNode.val)
        ansNode = ansNode.next
